package com.wbs.service.impl;

import com.wbs.mapper.TbSeckillGoodsMapper;
import com.wbs.model.TbSeckillGoods;
import com.wbs.model.TbSeckillGoodsExample;
import com.wbs.service.SeckillGoodsService;
import com.wbs.vo.SeckillGoodsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: 芝梦
 * @Description:
 * @DateTime: 2024/3/5 16:45
 */
@Service
public class SeckillGoodsServiceImpl implements SeckillGoodsService {
    @Autowired
    private TbSeckillGoodsMapper tbSeckillGoodsMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void getRedisHot() {
        Date date = new Date();
        TbSeckillGoodsExample tbSeckillGoodsExample = new TbSeckillGoodsExample();
        //开始时间小于当前时间, 结束时间大于当前时间
        tbSeckillGoodsExample.createCriteria()
                .andStartTimeLessThanOrEqualTo(date)
                .andEndTimeGreaterThanOrEqualTo(date);
        List<TbSeckillGoods> tbSeckillGoods = tbSeckillGoodsMapper.selectByExample(tbSeckillGoodsExample);

        //将集合中每一个元素转换为VO, 然后以 Hash 存入redis中, 大Key seckillGoodsList 小Key 每个元素的ID
        tbSeckillGoods.stream().map(
                x -> {
                    SeckillGoodsVO seckillGoodsVO = new SeckillGoodsVO();
                    BeanUtils.copyProperties(x, seckillGoodsVO);
                    return seckillGoodsVO;
                }
        ).forEach(y -> {
            redisTemplate.boundHashOps("seckillGoodsList").put(y.getId().toString(),y);
        });

    }

    @Override
    public List<SeckillGoodsVO> findSeckillGoodsList() {
        List<SeckillGoodsVO> seckillGoodsList = redisTemplate.boundHashOps("seckillGoodsList").values();
        return seckillGoodsList;
    }
}
